using System.Collections;
using System.Collections.Generic;
using com.yoozoo.gta.Extension.UGUI.ScreenOrientationEx;
using UnityEngine;
using UnityEngine.Playables;
using UnityEngine.Timeline;
using UnityEngine.UI;

namespace Yoozoo.Core.Timeline
{
    public class TimelineScreenOrientationController : MonoBehaviour
    {
        
        PlayableDirector playableDirector;
        public string[] cameraNames;
        public string[] cameraPortraitNames;
        public CanvasScaler canvasScaler;
        private Dictionary<string, TrackAsset> Name_BindTrack = new Dictionary<string, TrackAsset>();
        
        // Start is called before the first frame update
        void Awake()
        {
            OnInit();
        }

        public void OnInit()
        {
            if (playableDirector == null)
                playableDirector = this.gameObject.GetComponent<PlayableDirector>();
            
            //切换横竖屏相机
            if (cameraNames.Length > 0 && cameraPortraitNames.Length > 0)
            {
                GetAllTracks();
                SwitchCamera();
            }

            //刷新Canvas
            if (canvasScaler != null)
            {
                SwitchCanvas();
            }
            
            playableDirector.RebuildGraph();
        }

        private void SwitchCamera()
        {
            if (cameraNames.Length == 0 || cameraPortraitNames.Length == 0) return;
            
            bool isPortait = ScreenOrientManager.GetInstance().GetScreenOrientationIsPortrait();

            if (isPortait)
            {
                SwitchCameraByName(cameraNames, false);
                SwitchCameraByName(cameraPortraitNames, true);
            }
            else
            {
                SwitchCameraByName(cameraPortraitNames, false);
                SwitchCameraByName(cameraNames, true);
            }
        }

        private void SwitchCanvas()
        {
            if (canvasScaler == null) return;
            
            bool isPortait = ScreenOrientManager.GetInstance().GetScreenOrientationIsPortrait();
            if (isPortait)
            {
                canvasScaler.referenceResolution = new Vector2(750, 1334);
            }
            else
            {
                canvasScaler.referenceResolution = new Vector2(1334, 750);
            }
        }

        private void SwitchCameraByName(string[] trackNames, bool isActive)
        {
            foreach(string trackName in trackNames)
            {
                if (Name_BindTrack.TryGetValue(trackName, out var track)) track.muted = !isActive;
            }
        }
        
        private void GetAllTracks()
        {
            Name_BindTrack.Clear();
            TimelineAsset timelineAsset = (TimelineAsset)playableDirector.playableAsset;
            
            foreach (var track in timelineAsset.GetOutputTracks())
            {
                if (!Name_BindTrack.ContainsKey(track.name))
                {
                    Name_BindTrack.Add(track.name, track);
                }
            }
        }
    }
}
